let col1H = 0;
let col2H = 0;

Page({

    data: {
        scrollH: 0,
        imgWidth: 0,
        loadingCount: 0,
        images: [],
        col1: [],
        col2: [],
        page: 1,
        scrollTop: {
            scroll_top: 0,
            goTop_show: false
        },
        to_top_img:'/images/gototop.png'
    },

    onLoad: function() {
        wx.getSystemInfo({
            success: (res) => {
                let ww = res.windowWidth;
                let wh = res.windowHeight;
                let imgWidth = ww * 0.48;
                let scrollH = wh;

                this.setData({
                    scrollH: scrollH,
                    imgWidth: imgWidth
                });

                this.loadImages();
            }
        })
    },

    onImageLoad: function(e) {
        let imageId = e.currentTarget.id;
        let oImgW = e.detail.width; //图片原始宽度
        let oImgH = e.detail.height; //图片原始高度
        let imgWidth = this.data.imgWidth; //图片设置的宽度
        let scale = imgWidth / oImgW; //比例计算
        let imgHeight = oImgH * scale; //自适应高度

        let images = this.data.images;
        let imageObj = null;

        for (let i = 0; i < images.length; i++) {
            let img = images[i];
            if (img.id === imageId) {
                imageObj = img;
                break;
            }
        }

        imageObj.height = imgHeight;

        let loadingCount = this.data.loadingCount - 1;
        let col1 = this.data.col1;
        let col2 = this.data.col2;

        if (col1H <= col2H) {
            col1H += imgHeight;
            col1.push(imageObj);
        } else {
            col2H += imgHeight;
            col2.push(imageObj);
        }

        let data = {
            loadingCount: loadingCount,
            col1: col1,
            col2: col2
        };

        if (!loadingCount) {
            data.images = [];
        }

        this.setData(data);
    },

    loadImages: function() {
        let images = new Array();
        var obj_class = this;
        wx.showLoading({'title':'loading'});
        wx.request({
            url: 'https://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/' + obj_class.data.page,
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function(res) {
                if (res.data.results) {
                    for (var i = 0; i < res.data.results.length; i++) {
                        if (getApp().isEmpty(res.data.results[i].url) == false) {
                            let obj = new Object();
                            obj.pic = res.data.results[i].url;
                            obj.height = 0;
                            images[i] = obj;
                        }
                    }
                    console.log(images);
                    obj_class.setImages(images);
                    obj_class.data.page++;
                }
            },
            fail:function(res){
                wx.showToast('请求出错');
            },
            complete:function(res){
                wx.hideLoading();
            }
        })
    },
    setImages: function(images) {
        console.log(images);
        let baseId = "img-" + (+new Date());

        for (let i = 0; i < images.length; i++) {
            images[i].id = baseId + "-" + i;
        }

        this.setData({
            loadingCount: images.length,
            images: images
        });
    },
    onpenImg: function(e) {
        //console.log();
        wx.previewImage({
            current: '', // 当前显示图片的http链接
            urls: [e.currentTarget.dataset.img] // 需要预览的图片http链接列表
        })
    },
    scrollTopFun: function (e) {
        console.log(e.detail);
        if (e.detail.scrollTop > 300) {//触发gotop的显示条件 
            this.setData({
                'scrollTop.goTop_show': true
            });
            console.log(this.data.scrollTop)
        } else {
            this.setData({
                'scrollTop.goTop_show': false
            });
        }
    },
    //返回顶部
    goTopFun: function (e) {
        var _top = this.data.scrollTop.scroll_top;
        if (_top == 1) {
            _top = 0;
        } else {
            _top = 1;
        }
        this.setData({
            'scrollTop.scroll_top': _top
        });
        console.log("----");
        console.log(this.data.scrollTop)
    }
})